using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MSExcel = TcKs.MSOffice.Excel;

namespace TcKs.MSOffice.Samples.ExcelSample01 {
	public partial class Form1 : Form {
		public Form1() {
			InitializeComponent();
		}

		private void btnOpen_FilePath_Click( object sender, EventArgs e ) {
			using ( OpenFileDialog ofd = new OpenFileDialog() ) {
				ofd.Filter = "MS Excel files (*.xls)|*.xls|All files (*.*)|*.*";
				DialogResult rslt = ofd.ShowDialog( this );
				if ( rslt == DialogResult.OK ) {
					this.txtOpen_FilePath.Text = ofd.FileName;
				}
			}
		}

		private void btnOpen_Start_Click( object sender, EventArgs e ) {
			// The thread which communicate with MS Office must have a same culture as the MS Office.
			// It's a well-know bug for several versions of MS Office.
			// This bug is not in MS Office MUI.
			// My language mutation of MS Office is "en-US".
			System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo( "en-US" );

			using ( MSExcel.Application app = MSExcel.Application.CreateApplication() ) {
				MSExcel.Workbook book1 = app.Workbooks.Open( this.txtOpen_FilePath.Text );
				MSExcel.Worksheet sheet1 = book1.Worksheets[1];
				MSExcel.Range range_A1 = sheet1.GetRange( "A1" );
				MSExcel.Range range_B2 = sheet1.GetRange( "B2" );

				string message = "In cell 'A1' is '{0}' and in cell 'B2' is '{1}'.";
				message = string.Format( message, range_A1.Value, range_B2.Value );

				MessageBox.Show( message );
			}

			TcKs.MSOffice.Common.WrapperHelper.GCCollect();
		}
	}
}